//
//  WPMineInviteFriendsVC.m
//  wobingwoyi
//
//  Created by WGP on 16/8/6.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPMineInviteFriendsVC.h"
#import "WPCustomActionSheet.h"

@interface WPMineInviteFriendsVC ()<WPCustomActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UILabel *invidateCodeLabel;
@property (weak, nonatomic) IBOutlet UILabel *rewardRecodeLabel;
@property(nonatomic,strong)NSArray *sharedTypeArr;

@end

@implementation WPMineInviteFriendsVC

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    self.invidateCodeLabel.text = [WPUserAccountViewModel sharedInstance].userAccount.user.inviteCode;
    [self loadRewardRecord];
}

-(void)loadRewardRecord
{
    NSString *url = @"user/inviteGold.do";
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if([responseObject[@"result"] isEqualToString:@"success"])
        {
            if([responseObject[@"detail"] intValue])
            {
                self.rewardRecodeLabel.text = [NSString stringWithFormat:@"%@医票",responseObject[@"detail"]];
                self.rewardRecodeLabel.textColor = GLobalFontColor;
            }
            else
            {
                self.rewardRecodeLabel.text = @"暂无医票奖励";
            }
        }
        else
        {
            [self showErrorInfoWithStatus:@"网络错误"];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"网络错误"];
    }];
}

- (IBAction)back
{
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)invidateFriendsBtn
{
    WPCustomActionSheet *cusSheet = [[WPCustomActionSheet alloc] init];
    cusSheet.delegate = self;
    NSArray *contentArray = @[@{@"name":@"微信",@"icon":@"wechat"},
                              @{@"name":@"朋友圈 ",@"icon":@"FriendsCircle"},
                              @{@"name":@"QQ ",@"icon":@"iconfont-qq"},
                              @{@"name":@"QQ空间",@"icon":@"iconfont-qzone"},
                              @{@"name":@"短信",@"icon":@"duanxin"},
                              @{@"name":@"微博",@"icon":@"yaoqin_weibo"},
                              @{@"name":@"邮件",@"icon":@"email"}
                              ];
    
    [cusSheet showInView:[UIApplication sharedApplication].keyWindow contentArray:contentArray];
}

#pragma mark - YXCustomActionSheetDelegate

- (void) customActionSheetButtonClick:(UIButton *)btn
{
    
    // share type
    NSUInteger typeUI = 0;
    switch ((long)btn.tag) {
        case 0:
        {
            typeUI = [self.sharedTypeArr[0] unsignedIntegerValue];
        }
            break;
            
        case 1:
        {
            typeUI = [self.sharedTypeArr[1] unsignedIntegerValue];
        }
            break;
            
        case 2:
        {
            typeUI = [self.sharedTypeArr[2] unsignedIntegerValue];
        }
            break;
            
        case 3:
        {
            typeUI = [self.sharedTypeArr[3] unsignedIntegerValue];
        }
            break;
            
        case 4:
        {
            typeUI = [self.sharedTypeArr[4] unsignedIntegerValue];
        }
            break;
            
        case 5:
        {
            typeUI = [self.sharedTypeArr[5] unsignedIntegerValue];
        }
            break;
            
        case 6:
        {
            typeUI = [self.sharedTypeArr[6] unsignedIntegerValue];
        }
            break;
            
        case 7:
        {
            typeUI = [self.sharedTypeArr[7] unsignedIntegerValue];
        }
            break;
            
        default:
            break;
    }
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"邀请码:%@",self.invidateCodeLabel.text]
                                     images:[UIImage imageNamed:@"80"] //传入要分享的图片
                                        url:kAppStoreURL
                                      title:@"邀请好友拿医票"
                                       type:SSDKContentTypeAuto];
    
    [ShareSDK share:typeUI
         parameters:shareParams
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
         
         
         if (error)
         {
             WPLog(@"%@",error);
             
             if (error.code == 208)
             {
                 NSString *platform = nil;
                 if (btn.tag == 0 || btn.tag == 1 || btn.tag == 4)
                 {
                     platform = @"微信";
                 }
                 else if(btn.tag == 2 || btn.tag == 3)
                 {
                     platform = @"QQ";
                 }
                 
                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"您的设备没有安装%@",platform] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                 [alertView show];
             }
         }
         else
         {
             WPLog(@"%@",userData);
         }
         
     }];
}



-(NSArray *)sharedTypeArr
{
    if (_sharedTypeArr == nil)
    {
        _sharedTypeArr = @[
                         @(SSDKPlatformTypeWechat),
                         @(SSDKPlatformSubTypeWechatTimeline),
                         @(SSDKPlatformSubTypeQQFriend),
                         @(SSDKPlatformSubTypeQZone),
                         @(SSDKPlatformTypeSMS),
                         @(SSDKPlatformTypeSinaWeibo),
                         @(SSDKPlatformTypeMail),
                         ];
    }
    return _sharedTypeArr;
}

@end
